﻿<?php
include("class.base.php");
$html1 =<<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<title>$lang[lehe_pealkiri]</title>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW" />
<meta http-equiv="Content-Type" content="text/html; charset=utf8"/>
<meta http-equiv="Keywords" content="archive, estonian, kindred" />
<meta http-equiv="Content-Language" content="en" />
<meta name="language" content="english" />
<meta name="author" content="Rauno Moisto, Hannes Tamme, Mait Sarv, Kristjan Sarv, Allar Säks" />
<link rel="stylesheet" type="text/css" title="Brown" href="kujundus/style_brown.css" />
</head>
<body>
HTML;
$html2 =<<<HTML2
</body>
</html>
HTML2;
if (isset($_GET["materjal"]) and isset($_GET["materjal_id"]) and isset($_GET["sari"])) {
	include("resize.php");
	$_GET["sari"] = str_replace("..", "", $_GET["sari"]);
	if ($_GET["materjal"] == "foto" && (isset($_GET["album"]))) {
		$_GET["album"] = htmlspecialchars($_GET["album"], ENT_QUOTES);
		$piltide_nimekiri2 = array();
		$fotoParing = mysql_query("SELECT * FROM fotoalbum WHERE arhiivinumber='".$_GET["album"]."'");
		if (mysql_num_rows($fotoParing)) {
			$fotoRida = mysql_fetch_array($fotoParing);
			if (sisse_logitud()) {
				$fotoParing2 = mysql_query("SELECT * FROM foto WHERE fotoalbum='".$fotoRida["id"]."' ORDER BY arhiivinumber");
			}
			else {
				$fotoParing2 = mysql_query("SELECT * FROM foto WHERE oigused='avalik' AND fotoalbum='".$fotoRida["id"]."' ORDER BY arhiivinumber");
			}
			while ($fotoRida2 = mysql_fetch_array($fotoParing2)) {
				array_push($piltide_nimekiri2, $fotoRida2["arhiivinumber"]);
			}
		}
		$filedir = PHOTO_PATH.$_GET["sari"];
		if (is_dir($filedir)) {
			$handler = opendir($filedir);
			while ($file = readdir($handler)) {
				if (strstr($file, $_GET["album"])) {
					$filedir = PHOTO_PATH.$_GET["sari"]."/".$file;
					break;
				}
			}
		}
		if (is_dir($filedir)) {
			$piltide_nimekiri = array();
			$count = 0;
			foreach ($piltide_nimekiri2 as $baasiPilt) {
				$handler = opendir($filedir); //otsi üles albumi pildid
				while ($file = readdir($handler)) {
					if ($file != "." and $file != "..") {
						$isitimage = array_reverse(explode(".", $file));
						if ($isitimage[0] == "JPG" or $isitimage[0] == "jpg") {
							if ($_GET["materjal_id"] != 1 and strstr($file, $_GET["materjal_id"])) {
								$fullFilePath = $filedir."/".$file;
							}
							if (strstr($file, $baasiPilt)) {
								$piltide_nimekiri[] = $filedir."/".$file."#".$baasiPilt;
								$count++;
								break;
							}
						}
					}
				}
				closedir($handler);
			}
			sort($piltide_nimekiri);
			if ($fullFilePath) {
				$_GET["p"] = array_search($fullFilePath."#".$_GET["materjal_id"], $piltide_nimekiri);
			}
			if (!isset($_GET["p"])) {
				$p = 0;
			}
			else {
				$p = $_GET["p"];
			}
			$tykid = array_reverse(explode("#", $piltide_nimekiri[$p]));
			$query = mysql_fetch_array(mysql_query("SELECT * FROM foto WHERE arhiivinumber='".$tykid[0]."' LIMIT 1"));
			unset($tykid[0]);
			$piltide_nimekiri[$p] = implode("#", array_reverse($tykid));
			// $piltide_nimekiri[$p] on pildi täistee serveri kõhus
			// $originalFileName[0] on pildi originaalnimi koos laiendiga ja ilma mingi teeta
			/* eesmärk on kontrollida kas pilt serveri kõhus on uuem kui vähendatud koopia
			avalikus kataloogis; kui on uuem või kui avalikus kataloogis pole pilti siis tuleb teha
			vähendatud koopia ning salvestada avalikku kataloogi; */
			$originalFileName = array_reverse(explode("/", $piltide_nimekiri[$p]));
			if (!file_exists("jpeg_img/".$originalFileName[0]) || filemtime($piltide_nimekiri[$p]) > filemtime("jpeg_img/".$originalFileName[0])) {
				$image = new SimpleImage();
				$image->load($piltide_nimekiri[$p]);
				$height = IMAGE_HEIGHT_DEFAULT;
				$width = IMAGE_WIDTH_DEFAULT;
				if (isset($_GET["formaat"])) {
					$_CONSTANTS = get_defined_constants();
					if (isset($_CONSTANTS["IMAGE_WIDTH_".$_GET["formaat"]]) && isset($_CONSTANTS["IMAGE_HEIGHT_".$_GET["formaat"]])) {
						$height = $_CONSTANTS["IMAGE_HEIGHT_".$_GET["formaat"]];
						$width = $_CONSTANTS["IMAGE_WIDTH_".$_GET["formaat"]];
					}
				}
				$image->resizeToHeight($height);
				if ($image->getWidth() > $width) {
					$image->resizeToWidth($width);
				}
				$image->save("jpeg_img/".$originalFileName[0]);
			}
			if (isset($_GET["pisi"])) {
				header('Location: jpeg_img/'.$originalFileName[0]);
				die();
			}
			else {
				list($width, $height) = getimagesize('jpeg_img/'.$originalFileName[0]);
				if ($width < 400) { $width = 400; }
				echo $html1;
				echo "<div style='width: ".$width."px;'>";
				if ($count > 1) { echo pages("naita_pilt.php?materjal=foto&materjal_id=1&sari=".$_GET["sari"]."&album=".$_GET["album"]."&formaat=".$_GET["formaat"]."&p=%d", $p, $count, 1); }
				if (sisse_logitud()) { echo "&nbsp;&nbsp;<a href='naita_pilt_abi.php?path=".str_replace(PHOTO_PATH, 'PHOTO_PATH/', $piltide_nimekiri[$p])."' target='_blank'>".$lang["taissuurus"]."</a> "; }
				echo "<br />&nbsp;&nbsp;<a href='naita.php?t=foto&id=".$query["id"]."'><img src='jpeg_img/".$originalFileName[0]."' /></a><br />";
				echo "&nbsp;&nbsp;";
				foreach (get_must_show_columns("foto") as $col) {
					echo $query[$col]." ";
				}
				echo "</div>";
				echo $html2;
			}
		}
	}
	elseif ($_GET["materjal"] == "kasikiri") {
		$_GET["materjal_id"] = str_replace("..", "", $_GET["materjal_id"]);
		$filedir = KASIKIRI_PATH.$_GET["sari"]."/".$_GET["materjal_id"];
		if (is_dir($filedir)) {
			$handler = opendir($filedir); //otsi üles kõik käsikirja pildid
			$piltide_nimekiri = array();
			while ($file = readdir($handler)) {
				if ($file != "." and $file != "..") {
					$isitimage = array_reverse(explode(".", $file));
					if ($isitimage[0] == "JPG" or $isitimage[0] == "jpg" or $isitimage[0] == "PDF" or $isitimage[0] == "pdf") {
						array_push($piltide_nimekiri, $filedir."/".$file);
					}
				}
			}
			closedir($handler);
			$count = count($piltide_nimekiri);
			sort($piltide_nimekiri);
			if (!isset($_GET["p"])) {
				$p = 0;
			}
			else {
				$p = $_GET["p"];
			}
			$originalFileName = array_reverse(explode("/", $piltide_nimekiri[$p]));
			$fileExt = array_reverse(explode(".", $originalFileName[0]));
			if ($fileExt[0] == "PDF" or $fileExt[0] == "pdf") {
				if (!file_exists("pdf_doc/".$originalFileName[0]) || filemtime($piltide_nimekiri[$p]) > filemtime("pdf_doc/".$originalFileName[0])) {
					$execCommand = "cp ".$piltide_nimekiri[$p]." pdf_doc";
					exec($execCommand);
				}
				exec("touch pdf_doc/".$originalFileName[0]);
				header("Location: pdf_doc/".$originalFileName[0]);
				die();
			}
			else {
				if (!file_exists("jpeg_img/".$originalFileName[0]) || filemtime($piltide_nimekiri[$p]) > filemtime("jpeg_img/".$originalFileName[0])) {
					$image = new SimpleImage();
					$image->load($piltide_nimekiri[$p]);
					$height = IMAGE_HEIGHT_DEFAULT;
					$width = IMAGE_WIDTH_DEFAULT;
					if (isset($_GET["formaat"])) {
						$_CONSTANTS = get_defined_constants();
						if (isset($_CONSTANTS["IMAGE_WIDTH_".$_GET["formaat"]]) && isset($_CONSTANTS["IMAGE_HEIGHT_".$_GET["formaat"]])) {
							$height = $_CONSTANTS["IMAGE_HEIGHT_".$_GET["formaat"]];
							$width = $_CONSTANTS["IMAGE_WIDTH_".$_GET["formaat"]];
						}
					}
					$image->resizeToHeight($height);
					if ($image->getWidth() > $width) {
						$image->resizeToWidth($width);
					}
					$image->save("jpeg_img/".$originalFileName[0]);
				}
				list($width, $height) = getimagesize('jpeg_img/'.$originalFileName[0]);
				if ($width < 400) { $width = 400; }
				echo $html1;
				echo "<div style='width: ".$width."px;'>";
				if ($count > 1) { echo pages("naita_pilt.php?materjal=kasikiri&materjal_id=".$_GET["materjal_id"]."&sari=".$_GET["sari"]."&formaat=".$_GET["formaat"]."&p=%d", $p, $count, 1); }
				if (sisse_logitud()) { echo "&nbsp;&nbsp;<a href='naita_pilt_abi.php?path=".str_replace(KASIKIRI_PATH, 'KASIKIRI_PATH/', $piltide_nimekiri[$p])."' target='_blank'>".$lang["taissuurus"]."</a> "; }
				echo "<br /><img src='jpeg_img/".$originalFileName[0]."' /></div>";
				echo $html2;
			}
		}
	}
}
?>